package com.example.wuqujingdianphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.adapter.BCBookAdapter;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.BookListBean2;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BookSortListActivity extends BaseActivity {
    private static final String TAG = "BookSortListActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private String typeId;


    private BCBookAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_sort_list);
        ButterKnife.bind(this);

        typeId = getIntent().getStringExtra("typeId");
        String typeName = getIntent().getStringExtra("typeName");
        mTitle.setText(typeName);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initView();
        initData();

    }

    private void initView() {

        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new BCBookAdapter(this, null);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: position=" + position);
                Intent intent = new Intent(BookSortListActivity.this, BookDetailActivity.class);
                intent.putExtra("bookId", mAdapter.getItem(position).id);
                intent.putExtra("bookTitle", mAdapter.getItem(position).news_tit);
                startActivity(intent);

               /* Intent intent = new Intent(BookSortListActivity.this, BookReadActivity.class);
                intent.putExtra("pdfurl", mAdapter.getItem(position).url);
                startActivity(intent);*/
            }
        });


    }


    private void initData() {
        showLoading(this);
        //获取图书列表数据
        ViseHttp.GET("index.php/Jiekou/tushu/lm/" + typeId)
                .tag(TAG)
                .setHttpCache(true)
                //配置读取超时时间，单位秒
                .readTimeOut(60)
                //配置写入超时时间，单位秒
                .writeTimeOut(60)
                //配置连接超时时间，单位秒
                .connectTimeOut(60)
                //配置请求失败重试次数
                .retryCount(3)
                //配置请求失败重试间隔时间，单位毫秒
                .retryDelayMillis(1000)
                .request(new ACallback<List<BookListBean2>>() {
                    @Override
                    public void onSuccess(List<BookListBean2> bookListBeans) {
                        cancelLoading();
                        if (bookListBeans != null && bookListBeans.size() > 0) {
                            Log.i(TAG, "onSuccess: bookListBeans= " + bookListBeans.size());
                            mAdapter.setNewData(bookListBeans);

                        } else {
                            Log.i(TAG, "onSuccess: bookListBeans is null");
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cancelLoading();
                        //请求失败，errCode为错误码，errMsg为错误描述
                        ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                    }
                });

    }
}
